package jee.boot.module.delegated;

import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.StringUtils;
import jee.boot.module.crud.entity.DemoCrud;
import jee.boot.module.crud.service.DemoCrudService;
import jee.boot.entity.task.IexpTaskConfig;
import jee.boot.entity.task.IexpTask;
import jee.boot.entity.task.AbstractTemplateExportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DemoExport implements AbstractTemplateExportService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DemoCrudService demoCrudService;
    /**
     * 根据task查询当前导出任务需要的数据,
     *
     * @param task 当前任务
     * @return 返回数据约束为Map类型
     */
    @Override
    public Map<String, Object> queryData(IexpTask task) {
        Map<String, Object> map = new HashMap<>();
        String jsonParam = task.getJsonParam();
        DemoCrud demo = new DemoCrud();
        if (StringUtils.isNotBlank(jsonParam)) {
            try {
                demo = JsonMapper.fromJsonString(jsonParam, DemoCrud.class);
            } catch (Exception e) {
                logger.warn("解析参数失败,使用默认参数", e);
            }
        }
        map.put("list", demoCrudService.findList(demo));
        return map;
    }

    @Override
    public IexpTaskConfig taskConfig(IexpTask task) {
        IexpTaskConfig config = new IexpTaskConfig();
//        config.initExportByTemplateConfig("StandardModelExport.xlsx", "Demo导出");
        // debug测试时候可以使用下面这行配置模板,不用每次都重启服务了
        config.initExportByTemplatePathConfig("C:\\work\\idea-workspace\\java_framework\\backend\\jeeBoot\\business-center\\singleton-service\\src\\main\\resources\\templates\\excel\\StandardModelExport.xlsx", "Demo导出");
        return config;
    }
}
